package com.zlx.mall.biz.access.holder;

import com.alibaba.ttl.TransmittableThreadLocal;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author qingke
 **/
public class ContextHolder {

    private static final List<TransmittableThreadLocal<?>> CONTEXTS = new ArrayList<>();

    public static void register(TransmittableThreadLocal<?>... contexts) {
        if (contexts != null && contexts.length > 0) {
            CONTEXTS.addAll(Arrays.asList(contexts));
        }
    }

    public static void register(List<TransmittableThreadLocal<?>> contexts) {
        if (!CollectionUtils.isEmpty(contexts)) {
            CONTEXTS.addAll(contexts);
        }
    }

    public static void remove() {
        CONTEXTS.forEach(TransmittableThreadLocal::remove);
    }
}
